/*
 * AsignaturasAdminView.java
 *
 * Created on 24 de diciembre de 2007, 18:43
 */
package jschoolmanagerportable.gui;

import java.awt.Component;
import jschoolmanagerportable.gui.util.Dialogo;
import jschoolmanagerportable.model.Asignatura;
import jschoolmanagerportable.model.Colegio;
import jschoolmanagerportable.model.Curso;

/**
 *
 * @author  Administrador
 */
public class AsignaturasAdminView extends javax.swing.JInternalFrame {

    /** Creates new form AsignaturasAdminView */
    public AsignaturasAdminView() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = javax.persistence.Persistence.createEntityManagerFactory("JSchoolManagerPortablePU").createEntityManager();
        colegioQuery = entityManager.createQuery("SELECT c FROM Colegio c");
        colegioList = org.jdesktop.observablecollections.ObservableCollections.observableList(colegioQuery.getResultList());
        cursoQuery = entityManager.createQuery("SELECT c FROM Curso c");
        cursoList = org.jdesktop.observablecollections.ObservableCollections.observableList(cursoQuery.getResultList());
        asignaturaQuery = entityManager.createQuery("SELECT a FROM Asignatura a");
        asignaturaList = org.jdesktop.observablecollections.ObservableCollections.observableList(asignaturaQuery.getResultList());
        jLabel7 = new javax.swing.JLabel();
        colegiosCombo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        cursosCombo = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaAsignaturas = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        asignaturasIdText = new javax.swing.JTextField();
        asignaturasNombreText = new javax.swing.JTextField();
        asignaturasDescripcionText = new javax.swing.JTextField();
        asignaturasCantidadNotasText = new javax.swing.JTextField();
        botonGuardar = new javax.swing.JButton();
        botonBorrar = new javax.swing.JButton();
        botonNuevo = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jschoolmanagerportable.JSchoolManagerPortableApp.class).getContext().getResourceMap(AsignaturasAdminView.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        colegiosCombo.setName("colegiosCombo"); // NOI18N

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, colegioList, colegiosCombo);
        bindingGroup.addBinding(jComboBoxBinding);

        colegiosCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colegiosComboActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        cursosCombo.setName("cursosCombo"); // NOI18N

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cursoList, cursosCombo);
        bindingGroup.addBinding(jComboBoxBinding);

        cursosCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cursosComboActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tablaAsignaturas.setName("tablaAsignaturas"); // NOI18N
        tablaAsignaturas.getTableHeader().setReorderingAllowed(false);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, asignaturaList, tablaAsignaturas);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${id}"));
        columnBinding.setColumnName("Id");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nombre}"));
        columnBinding.setColumnName("Nombre");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${descripcion}"));
        columnBinding.setColumnName("Descripcion");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cantidaddenotas}"));
        columnBinding.setColumnName("Cantidaddenotas");
        columnBinding.setColumnClass(Integer.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(tablaAsignaturas);
        tablaAsignaturas.getColumnModel().getColumn(0).setPreferredWidth(10);
        tablaAsignaturas.getColumnModel().getColumn(1).setPreferredWidth(150);
        tablaAsignaturas.getColumnModel().getColumn(2).setPreferredWidth(150);
        tablaAsignaturas.getColumnModel().getColumn(3).setPreferredWidth(10);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        asignaturasIdText.setEditable(false);
        asignaturasIdText.setName("asignaturasIdText"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tablaAsignaturas, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.id}"), asignaturasIdText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        asignaturasNombreText.setName("asignaturasNombreText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tablaAsignaturas, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.nombre}"), asignaturasNombreText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        asignaturasDescripcionText.setName("asignaturasDescripcionText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tablaAsignaturas, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.descripcion}"), asignaturasDescripcionText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        asignaturasCantidadNotasText.setName("asignaturasCantidadNotasText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tablaAsignaturas, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.cantidaddenotas}"), asignaturasCantidadNotasText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        botonGuardar.setText(resourceMap.getString("botonGuardar.text")); // NOI18N
        botonGuardar.setName("botonGuardar"); // NOI18N
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        botonBorrar.setText(resourceMap.getString("botonBorrar.text")); // NOI18N
        botonBorrar.setName("botonBorrar"); // NOI18N
        botonBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarActionPerformed(evt);
            }
        });

        botonNuevo.setText(resourceMap.getString("botonNuevo.text")); // NOI18N
        botonNuevo.setName("botonNuevo"); // NOI18N
        botonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNuevoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(colegiosCombo, 0, 282, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cursosCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(asignaturasCantidadNotasText, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(asignaturasNombreText, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(asignaturasDescripcionText, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(asignaturasIdText, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 164, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(botonNuevo)
                            .addComponent(botonBorrar)
                            .addComponent(botonGuardar))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
                        .addContainerGap())))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonBorrar, botonGuardar, botonNuevo});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cursosCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel7)
                    .addComponent(colegiosCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonBorrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonNuevo))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(asignaturasIdText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(asignaturasNombreText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(asignaturasDescripcionText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(asignaturasCantidadNotasText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void colegiosComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colegiosComboActionPerformed

        Colegio colegio = (Colegio) colegiosCombo.getSelectedItem();
        if (colegio == null) {
            return;
        }
        cursoList.clear();
        cursoList.addAll(colegio.getCursoCollection());
    }//GEN-LAST:event_colegiosComboActionPerformed

    private void cursosComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cursosComboActionPerformed

        Curso curso = (Curso) cursosCombo.getSelectedItem();
        if (curso == null) {
            return;
        }
        asignaturaList.clear();
        asignaturaList.addAll(curso.getAsignaturaCollection());
        cursosCombo.repaint();
        tablaAsignaturas.repaint();
    }//GEN-LAST:event_cursosComboActionPerformed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed

        Asignatura asignatura = new Asignatura();
        asignatura.setId(Integer.parseInt(asignaturasIdText.getText()));
        asignatura.setNombre(asignaturasNombreText.getText());
        asignatura.setDescripcion(asignaturasDescripcionText.getText());
        asignatura.setCantidaddenotas(Integer.parseInt(asignaturasCantidadNotasText.getText()));
        Curso curso = (Curso) cursosCombo.getSelectedItem();
        asignatura.setIdcurso(curso);
        entityManager.getTransaction().begin();
        entityManager.merge(asignatura);
        entityManager.getTransaction().commit();
        entityManager.getTransaction().begin();
        entityManager.refresh(curso);
        entityManager.getTransaction().commit();
        asignaturasIdText.setText(String.valueOf(asignatura.getId()));        
}//GEN-LAST:event_botonGuardarActionPerformed

    private void botonBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarActionPerformed
        Asignatura asignatura = new Asignatura();
        asignatura.setId(Integer.parseInt(asignaturasIdText.getText()));
        asignatura.setNombre(asignaturasNombreText.getText());
        asignatura.setDescripcion(asignaturasDescripcionText.getText());
        asignatura.setCantidaddenotas(Integer.parseInt(asignaturasCantidadNotasText.getText()));
        Curso curso = (Curso) cursosCombo.getSelectedItem();
        asignatura.setIdcurso(curso);

        try {
            entityManager.getTransaction().begin();
            asignatura = entityManager.merge(asignatura);
            entityManager.remove(asignatura);
            entityManager.getTransaction().commit();
            asignaturaList.remove(asignatura);
            int row = asignaturaList.size() - 1;
            if (row != -1) {
                tablaAsignaturas.setRowSelectionInterval(row, row);
                tablaAsignaturas.scrollRectToVisible(tablaAsignaturas.getCellRect(row, 0, true));
            }else{
                limpiaCampos();
            }
            tablaAsignaturas.repaint();
        } catch (Exception e) {
            Dialogo.alert("Error", "No se pudo borrar la Asignatura", (Component) evt.getSource());
        }
}//GEN-LAST:event_botonBorrarActionPerformed

    private void botonNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNuevoActionPerformed

        Asignatura asignatura = new Asignatura();
        asignatura.setNombre("");
        asignatura.setCantidaddenotas(0);
        asignatura.setDescripcion("");
        Curso curso = (Curso) cursosCombo.getSelectedItem();
        asignatura.setIdcurso(curso);
        entityManager.getTransaction().begin();
        entityManager.persist(asignatura);
        entityManager.getTransaction().commit();
        asignaturaList.add(asignatura);
        int row = asignaturaList.size() - 1;
        tablaAsignaturas.setRowSelectionInterval(row, row);
        tablaAsignaturas.scrollRectToVisible(tablaAsignaturas.getCellRect(row, 0, true));
        tablaAsignaturas.repaint();

        asignaturasDescripcionText.setText("");
        asignaturasNombreText.setText("");
        asignaturasDescripcionText.setText("");
        asignaturasIdText.requestFocus();
}//GEN-LAST:event_botonNuevoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<jschoolmanagerportable.model.Asignatura> asignaturaList;
    private javax.persistence.Query asignaturaQuery;
    private javax.swing.JTextField asignaturasCantidadNotasText;
    private javax.swing.JTextField asignaturasDescripcionText;
    private javax.swing.JTextField asignaturasIdText;
    private javax.swing.JTextField asignaturasNombreText;
    private javax.swing.JButton botonBorrar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JButton botonNuevo;
    private java.util.List<jschoolmanagerportable.model.Colegio> colegioList;
    private javax.persistence.Query colegioQuery;
    private javax.swing.JComboBox colegiosCombo;
    private java.util.List<jschoolmanagerportable.model.Curso> cursoList;
    private javax.persistence.Query cursoQuery;
    private javax.swing.JComboBox cursosCombo;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaAsignaturas;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    public void limpiaCampos(){
        this.asignaturasCantidadNotasText.setText("");
        this.asignaturasDescripcionText.setText("");
        this.asignaturasIdText.setText("");
        this.asignaturasNombreText.setText("");
    }
}
